package cn.fl.customer.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import cmm.mid.core.framework.dto.BaseParamDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.user.facade.system.api.ISysUsrModifyApiService;
import cn.fl.customer.transform.CstCorpBscInfoVOTransform;
import cn.fl.customer.vo.*;
import cn.fl.framework.base.constant.WebBaseConstant;
import fl.customer.facade.customer.condition.*;
import fl.customer.facade.customer.dto.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.cert.api.ICertApiService;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cmm.comm.facade.upload.dto.SysUlFilesRecDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.service.ICstNpSocialRlService;
import cn.fl.customer.transform.CstBscInfoVOTransform;
import cn.fl.customer.transform.CstCreditVOTransform;
import cn.fl.customer.transform.CstNpBscInfoVOTransform;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjBscInfoQueryCondition;
import cn.fl.project.condition.PrjCstBscInfoQueryCondition;
import cn.fl.project.dto.PrjBscInfoDTO;
import cn.fl.project.dto.PrjCstBscInfoDTO;
import cn.fl.project.service.IProjectApiService;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.upload.constant.UploadConstant;

import com.alibaba.fastjson.JSON;

import fl.constant.facade.framework.constant.SystemConstant;
import fl.constant.facade.framework.constant.TableConstant;
import fl.customer.facade.customer.api.ICustomerAddApiService;
import fl.customer.facade.customer.api.ICustomerApiService;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.constant.CustomerConstant;
import fl.spl.facade.framework.enums.CommonOpTypCdEnum;
import fl.spl.facade.mfr.api.IMfrInfoModApiService;
import fl.spl.facade.mfr.api.IMfrInfoModDtlApiService;
import fl.spl.facade.mfr.condition.MfrInfoModDtlQC;
import fl.spl.facade.mfr.condition.MfrInfoModQC;
import fl.spl.facade.mfr.dto.MfrInfoModDTO;
import fl.spl.facade.mfr.dto.MfrInfoModDtlDTO;
import tc.esign.facade.esign.api.ICfcaUsrApiService;
import tc.esign.facade.esign.dto.req.user.ApiMobileRealNameValidateDTO;

/**
 * 客户基本信息服务实现
 *
 * @author FUJINGRUN
 */
@Service
public class CstBscInfoServiceImpl implements ICstBscInfoService {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(CstBscInfoServiceImpl.class);

    /**
     * 客户模块 API服务
     */
    @Autowired
    private ICustomerApiService customerApiService;

    /**
     * 客户查询API服务
     */
    @Autowired
    private ICustomerQueryApiService customerQueryApiService;

    /**
     * 客户新增API服务
     */
    @Autowired
    private ICustomerAddApiService customerAddApiService;

    /**
     * 本地数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    /**
     * 自然人社会关系服务
     */
    @Autowired
    private ICstNpSocialRlService cstNpSocialRlService;

    /**
     * 组织架构API
     */
    @Autowired
    private ISysOrgApiService sysOrgApiService;

    /**
     * 附件服务
     */
    @Autowired
    private ISysUlFilesRecService sysUlFilesRecService;

    /**
     * 自定义 API 暴露服务
     */
    @Autowired
    private IUploadApiService uploadApiService;

    /**
     * 身份验证
     */
    @Autowired
    private ICertApiService certApiService;

    @Autowired
    private ICfcaUsrApiService cfcaUsrApiService;

    /**
     * 用户更新 API 暴露服务
     */
    @Autowired
    private ISysUsrModifyApiService sysUsrModifyApiService;

    @Autowired
    private IMfrInfoModApiService mfrInfoModApiService; //厂商&客户变更

    @Autowired
    private IMfrInfoModDtlApiService mfrInfoModDtlApiService; //厂商&客户变更明细


    @Autowired
    private IProjectApiService projectApiService; //项目API


    @Override
    public CstBscInfoVO selectBy(Long id, String buOrgCd) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        ;
        CstBscInfoDTO dto = this.customerApiService.selectCstBscInfoById(id, buOrgCd);
        this.setCstIDImg(dto, id);
        CstBscInfoVO vo = CstBscInfoVOTransform.toVO(dto);
        //把客户关系转换成以分号分割的字符串
        if (null != dto) {
            Map<String, String> prjCtrlMap = new HashMap<>();
            List<String> codeGrpCdList = new ArrayList<>();
            codeGrpCdList.add(DictionaryConstant.CODE_GRP_CST_RL_TYP_CD);
            codeGrpCdList.add(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_PRJ_CTRL_CD);
            try {
                Map<String, Map<String, String>> remoteMap = sysDictionaryApiService.searchMultiGroupToMap(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
                if (CollectionUtils.isMapNotEmpty(remoteMap)) {
                    prjCtrlMap = remoteMap.get(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_PRJ_CTRL_CD);
                }
            } catch (Exception e) {
                BaseLogUtils.error(logger, "selectBy", "获取数据字典失败!");
            }
            StringBuffer cstRlAttr = new StringBuffer();
            List<CstRlAttrDTO> cstRlAttrDTOList = dto.getCstRlAttrDTOList();
            String prjCtrlCdNm = "";//如果有客户并且有这个值则赋值-只赋值一次
            String prjCtrlCd = "";//如果有客户并且有这个值则赋值-只赋值一次
            for (CstRlAttrDTO cstRlAttrDTO : cstRlAttrDTOList) {
                //要根据关系类型代码区分字段,区分后整理每个字段的值赋给对应的字段
                String rlTypCd = cstRlAttrDTO.getRlTypCd();
                if (StringUtils.isEmpty(rlTypCd)) {
                    continue;
                }

                if (rlTypCd.equals(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_CST_RL_TYP_CD_1)) {
                    //客户关系
                    cstRlAttr.append(cstRlAttrDTO.getRlVal());
                    cstRlAttr.append(";");
                } else if (rlTypCd.equals(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_CST_RL_TYP_CD_2)) {
                    String tempPrjCtrlCd = cstRlAttrDTO.getRlVal();
                    prjCtrlCd = tempPrjCtrlCd;
                    //这里如果取到一次值就不会在赋值了
                    if (StringUtils.isNotEmpty(tempPrjCtrlCd) && StringUtils.isEmpty(prjCtrlCdNm)) {
                        prjCtrlCdNm = prjCtrlMap.get(tempPrjCtrlCd);
                        cstRlAttrDTO.setRlValNm(prjCtrlCdNm);
                    }
                }
            }
            if (cstRlAttr.length() > 0) {
                cstRlAttr.deleteCharAt(cstRlAttr.length() - 1);
            }
            vo.setPrjCtrlCd(prjCtrlCd);
            vo.setPrjCtrlCdNm(prjCtrlCdNm);
            vo.setCstRlAttr(cstRlAttr.toString());
        }
        return vo;
    }

    @Override
    public Long add(CstBscInfoVO entity) throws BaseException {
        if (null == entity) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        //加入客户-用户关系表
        this.setCstUsrRVOInfo(entity);
        entity.setDsCd(WebBaseConstant.CHANNEL_CD);
        Long id = this.customerApiService.addCstBscInfo(CstBscInfoVOTransform.toDTO(entity));
        return id;
    }

    @Override
    public boolean removeById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        ;
        return this.customerApiService.removeCstBscInfoById(id);
    }

    @Override
    public boolean removeByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        ;
        return this.customerApiService.removeCstBscInfoByIds(ids);
    }

    @Override
    public boolean modify(CstBscInfoVO entity) throws BaseException {
        if (null == entity) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        ;
        this.setCstUsrRVOInfo(entity);
        Boolean flag = this.customerApiService.modifyCstBscInfo(CstBscInfoVOTransform.toDTO(entity));
        return flag;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        page = this.customerApiService.searchCstBscInfoListPage(page);
        List<CstBscInfoDTO> list = (List<CstBscInfoDTO>) page.getResultList();

        //查询对应的主要信息变更流程状态
        if (CollectionUtils.isNotEmpty(list)) {
            //遍历客户信息
            for (CstBscInfoDTO dto : list) {
                boolean flag = true;
                Long cId = dto.getId();
                if (StringUtils.isNotEmpty(cId)) {
                    //查询是否有未结束的客户主要信息变更流程
                    MfrInfoModQC mfrInfoModQC = new MfrInfoModQC();
                    List<String> audStsCdList = new ArrayList<String>();
                    audStsCdList.add("1"); //审核中
                    audStsCdList.add("4"); //驳回呈报人
                    mfrInfoModQC.setAudStsCdList(audStsCdList);
                    mfrInfoModQC.setMfrId(cId); //客户ID
                    mfrInfoModQC.setIsNew(BaseConstant.IS_YESNO_YES);
                    mfrInfoModQC.setIsDel(BaseConstant.IS_YESNO_NO);
                    mfrInfoModQC.setIsValid(BaseConstant.IS_YESNO_YES);
                    List<MfrInfoModDTO> mfrInfoModDTOs = this.mfrInfoModApiService.searchMfrInfoModList(mfrInfoModQC);
                    if (CollectionUtils.isNotEmpty(mfrInfoModDTOs) && mfrInfoModDTOs.size() > 0) {
                        flag = false;
                        dto.setAudStsCd("11"); //主要信息变更流程中
                    }

                    if (flag) {
                        //查询该客户是否已立项
                        PrjBscInfoQueryCondition qc = new PrjBscInfoQueryCondition();
                        qc.setCstId(cId);
                        List<PrjBscInfoDTO> prjList = this.projectApiService.searchPrjBscInfoList(qc);
                        if (CollectionUtils.isNotEmpty(prjList) && prjList.size() > 0) {//有项目
                            dto.setAudStsCd("13"); //已立项
//							//查询项目是否已发起评审
//							SysFlowSHQueryCondition condition = new SysFlowSHQueryCondition();
//							condition.setWordsInTheTable(PrjBscInfoConstant.PRJ_FLOW_S_H_PRJ);
//							condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
//							condition.setBusTyp("001");//立项
//							for(PrjBscInfoDTO pDto : prjList){
//								Long prjId = pDto.getId();//项目ID
//								if(StringUtils.isNotEmpty(prjId)){
//									condition.setDataId(pDto.getId());
//									List<SysFlowSHDTO> flowList = this.flowQueryApi.searchFlowSHList(condition);
//									if(CollectionUtils.isNotEmpty(flowList) && flowList.size()>0){
//										//查询支付表是否有起租日-即是否已放款
//										PrjPrdPaySchMQueryCondition payQc = new PrjPrdPaySchMQueryCondition();
//										payQc.setPrjId(prjId);
//										payQc.setIsStartRent(1);//已起租（起租日不为空）
//										List<PrjPrdPaySchMDTO> payList = this.prjPrdPaySchMApiService.searchPrjPrdPaySchMList(payQc);
//										if(CollectionUtils.isNotEmpty(payList) && payList.size()>0){//已放款
//											dto.setAudStsCd("12"); //项目已放款
//										}
//									}
//								}
//							}
                        } else {
                            //查询该客户是否有担保项目
                            PrjCstBscInfoQueryCondition prjCstBscInfoQc = new PrjCstBscInfoQueryCondition();
                            prjCstBscInfoQc.setCstId(cId); //客户ID
                            prjCstBscInfoQc.setCstRlTyp("2"); //客户关系：2 担保人
                            List<PrjCstBscInfoDTO> prjCstBscInfoList = this.projectApiService.searchPrjCstBscInfoList(prjCstBscInfoQc, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
                            if (CollectionUtils.isNotEmpty(prjCstBscInfoList) && prjCstBscInfoList.size() > 0) {//有项目
                                dto.setAudStsCd("14"); //有担保项目
                            }
                        }
                    }
                }
            }
        }

        page.setResultList(CstBscInfoVOTransform.toVOList(list));
        return page;
    }

    @Override
    public List<KeyValueVO> searchCstStsCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_STS_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchCstTypCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_TYP_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchBusinessLicenceList() throws BaseException {
        List<KeyValueVO> kvList = new ArrayList<>();
        KeyValueVO keyValueVO = new KeyValueVO();
        keyValueVO.setKey("1");
        keyValueVO.setValue("到期");
        kvList.add(keyValueVO);

        KeyValueVO keyValueVO1 = new KeyValueVO();
        keyValueVO1.setKey("2");
        keyValueVO1.setValue("即将到期");
        kvList.add(keyValueVO1);

        KeyValueVO keyValueVO2 = new KeyValueVO();
        keyValueVO2.setKey("3");
        keyValueVO2.setValue("其它");
        kvList.add(keyValueVO2);
        return kvList;
    }

    @Override
    public List<KeyValueVO> searchCstLvlCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_LVL_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchCertTypCdList(String type) throws BaseException {
        //这里要区分自然人证件和法人证件
        return this.sysDictionaryApiService.getKvListWithAttr1(DictionaryConstant.CODE_GRP_CERT_TYP_CD, type, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchCertTypCdListZj(String attr1, String attr2)
            throws BaseException {
        return this.sysDictionaryApiService.getKvListWithAttr1AndAttr2(DictionaryConstant.CODE_GRP_CERT_TYP_CD, attr1, attr2, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchEduLvlCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_EDU_LVL_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchIndCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_IND_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchMarStsCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_MAR_STS_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchGenderCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_GENDER_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchCorpTypCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CORP_TYP_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchRegCapUntCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_REG_CAP_UNT_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchPaidCapUntCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_REG_CAP_UNT_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchMTypCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_M_TYP_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchEmpQtyCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_EMP_QTY_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchGrpRlTypCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_RL_TYP_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchIsActualPayerCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList("CST0062", CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }


    @Override
    public boolean checkCstNpSocialRlMarInfo(Long cstId, String rlTypCd) throws BaseException {
        if (StringUtils.isEmpty(cstId) || StringUtils.isEmpty(rlTypCd)) return false;
		/*Pager page = new Pager();
		page.setCurrentPage(1);
		page.setEveryPage(Integer.MAX_VALUE);*/
        List<Long> cstIdList = new ArrayList<>();
        cstIdList.add(cstId);
        CstNpSocialRlQueryCondition cstNpSocialRlQueryCondition = new CstNpSocialRlQueryCondition();
        cstNpSocialRlQueryCondition.setRlTypCd(rlTypCd);
        cstNpSocialRlQueryCondition.setCstIdList(cstIdList);
        List<CstNpSocialRlDTO> list = this.cstNpSocialRlService.searchList(cstNpSocialRlQueryCondition);
        return list.size() > 0 ? true : false;
    }

    @Override
    public List<KeyValueVO> searchBusinessOrgList() throws BaseException {
        List<SysOrgDTO> list = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
        List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
        if (CollectionUtils.isEmpty(list)) {
            return kvList;
        }
        for (SysOrgDTO sysOrgDTO : list) {
            KeyValueVO kv = new KeyValueVO();
            kv.setKey(sysOrgDTO.getOrgCd());
            kv.setValue(sysOrgDTO.getOrgNm());
            kvList.add(kv);
        }
        return kvList;
    }


    @Override
    public CstBscInfoVO selectDetailById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        CstBscInfoDTO dto = this.customerApiService.selectCstBscInfoDetailById(id,
                CurrentThreadContext.getFlServiceSysCd(),
                CurrentThreadContext.getCurrentOrgCd(),
                CurrentThreadContext.getCurrentBuOrgCd());

        //把客户关系转换成以分号分割的字符串
        CstBscInfoVO vo = setCstBscDynamicInfo(id, dto);
        return vo;
    }

    @Override
    public CstBscInfoVO selectDetailBy(Long id, String orgCd, String buOrgCd) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        CstBscInfoDTO dto = this.customerApiService.selectCstBscInfoDetailById(id,
                CurrentThreadContext.getFlServiceSysCd(),
                orgCd,
                buOrgCd);

        //把客户关系转换成以分号分割的字符串
        CstBscInfoVO vo = setCstBscDynamicInfo(id, dto);
        return vo;
    }

    //设置动态字段信息
    private CstBscInfoVO setCstBscDynamicInfo(Long id, CstBscInfoDTO dto) {
        StringBuffer cstRlAttr = new StringBuffer();
        StringBuffer cstRlAttrNm = new StringBuffer();
        String prjCtrlCdNm = "";//如果有客户并且有这个值则赋值-只赋值一次
        String prjCtrlCd = "";//如果有客户并且有这个值则赋值-只赋值一次

        String cstLvlCdNm = "";//客户级别名称
        String cstLvlCd = "";//客户级别
        List<CstRlAttrDTO> cstRlAttrDTOList = dto.getCstRlAttrDTOList();
        if (null != dto) {
            this.setCstIDImg(dto, id);
            Map<String, String> cstRlTypMap = new HashMap<>();
            Map<String, String> prjCtrlMap = new HashMap<>();
            Map<String, String> cstLvlMap = new HashMap<>();
            List<String> codeGrpCdList = new ArrayList<>();
            codeGrpCdList.add(DictionaryConstant.CODE_GRP_CST_RL_TYP_CD);
            codeGrpCdList.add(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_PRJ_CTRL_CD);
            codeGrpCdList.add(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_CST_LVL_CD);
            try {
                Map<String, Map<String, String>> remoteMap = sysDictionaryApiService.searchMultiGroupToMap(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
                if (CollectionUtils.isMapNotEmpty(remoteMap)) {
                    cstRlTypMap = remoteMap.get(DictionaryConstant.CODE_GRP_CST_RL_TYP_CD);
                    prjCtrlMap = remoteMap.get(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_PRJ_CTRL_CD);
                    cstLvlMap = remoteMap.get(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_CST_LVL_CD);
                }
            } catch (Exception e) {
                BaseLogUtils.error(logger, "setCstBscDynamicInfo", "获取数据字典失败!");
            }


            for (CstRlAttrDTO cstRlAttrDTO : cstRlAttrDTOList) {
                //要根据关系类型代码区分字段,区分后整理每个字段的值赋给对应的字段
                String rlTypCd = cstRlAttrDTO.getRlTypCd();
                if (StringUtils.isEmpty(rlTypCd)) {
                    continue;
                }

                if (rlTypCd.equals(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_CST_RL_TYP_CD_1)) {
                    //客户关系
                    cstRlAttr.append(cstRlAttrDTO.getRlVal());
                    cstRlAttr.append(";");
                    String cstRlCd = cstRlAttrDTO.getRlVal();
                    if (cstRlTypMap.containsKey(cstRlCd)) {
                        cstRlAttrNm.append(cstRlTypMap.get(cstRlCd));
                        cstRlAttrNm.append(";");
                    }
                } else if (rlTypCd.equals(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_CST_RL_TYP_CD_2)) {
                    String tempPrjCtrlCd = cstRlAttrDTO.getRlVal();
                    prjCtrlCd = tempPrjCtrlCd;
                    //这里如果取到一次值就不会在赋值了
                    if (StringUtils.isNotEmpty(tempPrjCtrlCd) && StringUtils.isEmpty(prjCtrlCdNm)) {
                        prjCtrlCdNm = prjCtrlMap.get(tempPrjCtrlCd);
                        cstRlAttrDTO.setRlValNm(prjCtrlCdNm);
                    }
                } else if (rlTypCd.equals(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_CST_RL_TYP_CD_3)) {
                    String val = cstRlAttrDTO.getRlVal();
                    cstLvlCd = val;
                    //这里如果取到一次值就不会在赋值了
                    if (StringUtils.isNotEmpty(val) && StringUtils.isEmpty(cstLvlCdNm)) {
                        cstLvlCdNm = cstLvlMap.get(val);
                        cstRlAttrDTO.setRlValNm(cstLvlCdNm);
                    }
                }
            }
            if (cstRlAttr.length() > 0) {
                cstRlAttr.deleteCharAt(cstRlAttr.length() - 1);
            }
        }
        CstBscInfoVO vo = CstBscInfoVOTransform.toVO(dto);
        vo.setCstRlAttr(cstRlAttr.toString());
        vo.setCstRlAttrNm(cstRlAttrNm.toString());
        vo.setPrjCtrlCd(prjCtrlCd);
        vo.setPrjCtrlCdNm(prjCtrlCdNm);
        vo.setCstLvlCd(cstLvlCd);//设置客户级别
        return vo;
    }

    @Override
    public List<CstBscInfoVO> checkCstBscInfoNpIsHere(String certNm, String certNo, String certTypCd, String sysCd) throws BaseException {

        if (StringUtils.isEmpty(sysCd)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CMM_SYS_CD_NOT_EMPTY);
        }
        ;

        if (StringUtils.isEmpty(certNm)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "客户名称不能为空！");
        }
        ;

        if (StringUtils.isEmpty(certNo)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "客户名称不能为空！");
        }

        if(DictionaryConstant.CODE_CERT_TYP_CD_0.equals(certTypCd) && !StringUtils.isIdCardNumber(certNo)){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请输入正确的证件号码！");
        }
        ;

        List<CstBscInfoDTO> cstBscInfoDTOList = this.customerApiService.checkCstBscInfoNpIsHere(certNm, certNo, certTypCd, sysCd);
        return CstBscInfoVOTransform.toVOList(cstBscInfoDTOList);
    }


    /**
     * 设置客户-用户关系信息
     *
     * @param entity
     */
    private void setCstUsrRVOInfo(CstBscInfoVO entity) {
        CstUsrRVO cstUsrRVO = new CstUsrRVO();
        cstUsrRVO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
        cstUsrRVO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        cstUsrRVO.setOpTm(new Date());
        cstUsrRVO.setOpUsrId(CurrentThreadContext.getCurrentUserId());
        entity.setCstUsrRVO(cstUsrRVO);
    }

    @Override
    public List<CstBscInfoVO> checkCstBscInfoCorpIsHere(String certNm, String certNo, String certTypCd, String sysCd)
            throws BaseException {
        if (StringUtils.isEmpty(sysCd)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CMM_SYS_CD_NOT_EMPTY);
        }
        ;
		
		/*if(StringUtils.isEmpty(certNm)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "客户名称不能为空！");
		};*/

        if (StringUtils.isEmpty(certNo)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "证件号码不能为空！");
        }
        ;

        if (StringUtils.isEmpty(certTypCd)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "证件类型不能为空！");
        }
        ;

        List<CstBscInfoDTO> cstBscInfoDTOList = this.customerApiService.checkCstBscInfoCorpIsHere(certNm, certNo, certTypCd, sysCd);
        return CstBscInfoVOTransform.toVOList(cstBscInfoDTOList);
    }

    @Override
    public List<CstNpBscInfoVO> searchCstNpBscInfoWithMp(CstNpBscInfoQueryCondition qc) throws BaseException {

        if (null == qc) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CMM_QUERY_OBJ_EMPTY);
        }

        if (StringUtils.isBlank(qc.getSysCd())) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CMM_SYS_CD_NOT_EMPTY);
        }

        String mp1 = qc.getMp1();
        String mp2 = qc.getMp2();

        //获取手机号验证
        if (StringUtils.isBlank(mp1) || !StringUtils.checkMobileNumber(mp1)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CUST_VALID_MP1);
        }

        if (StringUtils.isNotBlank(mp2) && !StringUtils.checkMobileNumber(mp2)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CUST_VALID_MP2);
        }

        if (StringUtils.isNotBlank(mp1) && StringUtils.isNotBlank(mp2)) {
            if (mp1.equals(mp2)) {
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CUST_VALID_MP1_COM_MP2);
            }
        }

        List<CstNpBscInfoDTO> list = this.customerApiService.searchCstNpBscInfoWithMp(qc);

        return CstNpBscInfoVOTransform.toVOList(list);
    }

    @Override
    public List<CstBscInfoVO> searchCstBscInfoWithCertNo(CstBscInfoQueryCondition qc) throws BaseException {
        if (null == qc) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CMM_QUERY_OBJ_EMPTY);
        }

        if (StringUtils.isBlank(qc.getSysCd())) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CMM_SYS_CD_NOT_EMPTY);
        }

        if (StringUtils.isBlank(qc.getCertNo())) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, SysLogConstant.LOG_CUST_VALID_CERT_NO_EMPTY);
        }
        List<CstBscInfoDTO> list = this.customerApiService.searchCstBscInfoWithCertNo(qc);

        return CstBscInfoVOTransform.toVOList(list);
    }

    @Override
    public JSONArray searchCstRlTypArray() throws BaseException {
        List<KeyValueVO> list = this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_RL_TYP_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
        JSONArray cstRlTypCdJA = JSONArray.fromObject(list.toArray());
        return cstRlTypCdJA;
    }

    @Override
    public List<KeyValueVO> searchCstRlTypList() throws BaseException {
        List<KeyValueVO> list = this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_RL_TYP_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
        return list;
    }

    @Override
    public List<KeyValueVO> searchOcpCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_OCP_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchPosCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_POS_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchTitCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_TIT_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public JSONArray searchTreeByCd(String codeGrpCd, String pCodeInitVal) throws BaseException {
        JSONArray jsonArray = new JSONArray();
        if (StringUtils.isEmpty(codeGrpCd)) {
            return jsonArray;
        }
        String sysCd = CurrentThreadContext.getFlServiceSysCd();
        String orgCd = CurrentThreadContext.getCurrentOrgCd();
        List<SysDictionaryDTO> dicList = this.sysDictionaryApiService.searchGroup(sysCd, orgCd, codeGrpCd);
        if (CollectionUtils.isEmpty(dicList)) {
            return jsonArray;
        }
        for (SysDictionaryDTO sysDictionaryDTO : dicList) {
            JSONObject obj = new JSONObject();
            obj.put("id", sysDictionaryDTO.getCode());
            obj.put("text", sysDictionaryDTO.getCodeNm());
            obj.put("pid", sysDictionaryDTO.getPCode());
            jsonArray.add(obj);
        }
        JSONArray treeList = this.treeList(jsonArray, pCodeInitVal);
        return treeList;
    }


    /**
     * 递归添加整个集合数据
     *
     * @param treeList
     * @param parentId
     * @return
     */
    private JSONArray treeList(JSONArray treeList, String parentId) {
        JSONArray childData = new JSONArray();
        for (Object object : treeList) {
            JSONObject jsonData = JSONObject.fromObject(object);
            String oid = jsonData.getString("id");
            String pid = jsonData.getString("pid");
            if (parentId.equals(pid)) {
                JSONArray c_node = treeList(treeList, oid);
                jsonData.put("children", c_node);
                childData.add(jsonData);
            }
        }
        return childData;
    }

    @Override
    public List<KeyValueVO> searchYNList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_SYS_YES_NO_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchValidStsList() {
        return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_VALID_STATUS_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<CstCreditVO> searchCreditList(CstCreditQC cstCreditQC) throws BaseException {
        List<CstCreditDTO> list = this.customerQueryApiService.searchCreditList(cstCreditQC);
        return CstCreditVOTransform.toVOList(list);
    }

    @Override
    public List<KeyValueVO> searchPayTypCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_BACK_PAY_TYP_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchHighDgCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_HIGH_DG_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchLivCndCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_LIV_CND_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<KeyValueVO> searchZxCstLvlCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(fl.customer.facade.framework.constant.DictionaryConstant.CODE_GRP_ZX_CST_LVL_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public Map<String, List<KeyValueVO>> searchCustCstBacInfoNpAddPageDicInfo(List<String> grpCdList) throws BaseException {

        return null;
    }

    @Override
    public CstBscInfoVO selectById(Long id) throws BaseException {
        CstBscInfoDTO result = this.customerQueryApiService.getCstBscInfoById(id);
        return CstBscInfoVOTransform.toVO(result);
    }

    @Override
    public CstNpSocialRlDTO searchCstNpSocialRlList(Long cstId, String rlTypCd) throws BaseException {
        CstNpSocialRlQueryCondition condition = new CstNpSocialRlQueryCondition();
        condition.setRlTypCd(rlTypCd);
        List<Long> cstIdList = new ArrayList<>();
        cstIdList.add(cstId);
        condition.setCstIdList(cstIdList);
        List<CstNpSocialRlDTO> list = this.customerApiService.searchCstNpSocialRlList(condition);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    /**
     * @MethodName checkMpAndCert
     * @Description: 三要素校验
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/6/22 17:15
     * @Version: V1.0.0
     * @Param usrNm
     * @Param certNo
     * @Param mp
     * @Return java.lang.Boolean
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public Boolean checkMpAndCert(String usrNm, String certNo, String mp) throws BaseException {
        BaseLogUtils.info(logger, "checkMpAndCert", "手机三要素校验开始:usrNm:"+usrNm+",certNo:"+certNo+",mp:"+mp);
        Boolean rtn = false;
        if (StringUtils.isEmpty(usrNm) || StringUtils.isEmpty(certNo) || StringUtils.isEmpty(mp)) {
            BaseLogUtils.info(logger, "checkMpAndCert", "手机号三要素：参数不能为空! ", CurrentThreadContext.getCurrentUserName());
            return rtn;
        }
        BaseParamDTO baseParam = new BaseParamDTO();
        ApiMobileRealNameValidateDTO mobileRealNameValidate = new ApiMobileRealNameValidateDTO();
        try {
            baseParam.setSysCd(CurrentThreadContext.getCurrentSysCd()); //系统代码
            baseParam.setChannelCd("CHL0001"); //渠道代码：融资租赁后台管理
            baseParam.setServiceCd(CurrentThreadContext.getCurrentSysCd()); //服务代码--暂用
            baseParam.setFrontCd(CurrentThreadContext.getCurrentSysCd()); //前端代码--暂用

            mobileRealNameValidate.setUserName(usrNm);
            mobileRealNameValidate.setIdentificationNo(certNo);
            mobileRealNameValidate.setMobilePhone(mp);
            mobileRealNameValidate.setBussinessTypeCd("CUST"); //业务代码:客户 CUST

            DataResultDTO result = this.cfcaUsrApiService.mobileRealNameValidate(mobileRealNameValidate, baseParam);
            if (BaseConstant.IS_YESNO_YES == result.getSuccess()) {
                rtn=true;
                BaseLogUtils.info(logger, "checkThreeElements", "手机号三要素：验证通过! mp:"+mp);
            } else {
                BaseLogUtils.info(logger, "checkThreeElements", "手机号三要素：验证不通过! mp:"+mp);
            }
        } catch (BaseException ex) {
            BaseLogUtils.error(logger, "checkThreeElements", "验证手机号三要素操作失败! =>"+ex.getMessage(), ex);
        } catch (Exception ex) {
            BaseLogUtils.error(logger, "checkThreeElements", "验证手机号三要素操作失败! =>"+ex.getMessage(), ex);
        }
        BaseLogUtils.info(logger, "checkThreeElements", mp+"手机号三要素：验证结果"+rtn);
        return rtn;
    }

    @Override
    public CstValidCardInfoVO validateCstCert(String certNm, String certNo) throws BaseException {
        CstValidCardInfoVO cstValidCardInfoVO = new CstValidCardInfoVO();
        cstValidCardInfoVO.setSuccess(false);// 默认值
        cstValidCardInfoVO.setAbsolutePath("");
        cstValidCardInfoVO.setRelativePath("");
        // 加入开关
        String isOpen = PropertiesCacheUtils.getProperty(CustomerConstant.CUSTOMER_CUSTOMER_PROPERTIES_YT_OPEN);
        if (true == Boolean.valueOf(isOpen)) {
            BaseLogUtils.info(logger, "validateCstCert", "身份验证功能已开启. certNo:" + certNo + "-certNm:" + certNm);
            String serverPath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_SERVER_PATH);//服务地址
            String rootPath = PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_SERVER_PATH);
            //首先根据客户证件号码获取本地库是否存在,如果存在验证名称是否正确,不正确提示验证未通过; 否则证件号码查不到则进行三方验证,三方通过后将插入到本地库。
            try {
                CstCertRecQC qc = new CstCertRecQC();
                qc.setCertNo(certNo);
                qc.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);
                List<CstCertRecDTO> certList = this.customerQueryApiService.searchCstCertRecList(qc);
                if (CollectionUtils.isNotEmpty(certList)) {
                    this.setCstCertImgInfo(certList);
                    CstCertRecDTO certT = certList.get(0);
                    int certSize = certList.size();
                    if (certSize > 1) {
                        BaseLogUtils.error(logger, "validateCstCert", "根据条件" + JSON.toJSONString(qc) + "查询出来结果数量:" + certList.size());
                    }
                    if (null != certT) {
                        String certNmt = certT.getCertNm();
                        if (certNm.equals(certNmt)) {
                            //说明证件有法律效应, 可以直接返回
                            cstValidCardInfoVO.setSuccess(true);
                            String imgUrl = certT.getImgUrl();
                            if (StringUtils.isNotEmpty(imgUrl)) {
                                if (imgUrl.contains(rootPath)) {
                                    imgUrl = imgUrl.replace(rootPath, "");
                                }
                                cstValidCardInfoVO.setAbsolutePath(imgUrl);
                                cstValidCardInfoVO.setRelativePath(serverPath + imgUrl);
                            } else {
                                cstValidCardInfoVO.setAbsolutePath("");
                                cstValidCardInfoVO.setRelativePath("");
                            }
                            return cstValidCardInfoVO;
                        }
                    } else {
                        BaseLogUtils.error(logger, "validateCstCert", "根据条件" + JSON.toJSONString(qc) + "查询出来结果对象为null");
                    }
                }
            } catch (BaseException e) {
                BaseLogUtils.info(logger, "validateCstCert", e.getMessage());
            } catch (Exception e) {
                BaseLogUtils.error(logger, "validateCstCert", "验证本地身份证接口异常!");
            }

            BaseLogUtils.info(logger, "validateCstCert", "第三方验证身份证功能已开启!");
            try {
                DataResultDTO dataResultDTO = this.certApiService.validCert(certNm, certNo);
                System.out.println("==身份证验证结果=> " + JSON.toJSONString(dataResultDTO));
                boolean success = false;
                if (null != dataResultDTO && StringUtils.isNotEmpty(dataResultDTO.getCode()) && String.valueOf(BaseConstant.IS_YESNO_YES).equals(dataResultDTO.getCode())) {
                    success = true;
                }
                //转图片
                String imgContent = null == dataResultDTO.getData() ? "" : String.valueOf(dataResultDTO.getData());
                String imgFilePath = "";
                if (StringUtils.isNotEmpty(imgContent)) {
                    try {
                        // Base64解码
                        byte[] b = Base64.decodeBase64(imgContent);
                        // 生成JPG图片
                        imgFilePath = this.getFileSavePath("customer");
                        String randomFileName = UUID.randomUUID().toString().replace("-", "");
                        imgFilePath += randomFileName + ".jpg";
                        OutputStream out = new FileOutputStream(imgFilePath);
                        out.write(b);
                        out.flush();
                        out.close();

                        String prifixPath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_ROOT_PATH);//根目录
                        imgFilePath = imgFilePath.replace(prifixPath, "");//去掉前缀
                        cstValidCardInfoVO.setAbsolutePath(imgFilePath);
                        cstValidCardInfoVO.setRelativePath(serverPath + imgFilePath);
                        out = null;
                    } catch (Exception e) {
                        e.getStackTrace();
                        BaseLogUtils.error(logger, "validateIdCard", "保存自然人客户身份证照片失败!certNo:" + certNo + "-certNm:" + certNm);
                    }
                }
                if (success) {
                    try {
                        //加入到身份证记录表
                        CstCertRecDTO dto = new CstCertRecDTO();
                        dto.setCertNm(certNm);
                        dto.setCertNo(certNo);
                        dto.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);
                        dto.setCertImg(imgContent);
                        dto.setCrtTm(new Date());
                        dto.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
                        Long id = this.customerAddApiService.addCstCertRec(dto);
                        //加入客户身份正照片附件
                        this.uploadCstIDImg(imgFilePath, certNm, certNo, id);
                    } catch (Exception e) {
                        BaseLogUtils.error(logger, "validateIdCard", "保存自然人客户身份证数据到本地数据库失败!certNo:" + certNo + "-certNm:" + certNm);
                    }
                }
                cstValidCardInfoVO.setSuccess(success);
                return cstValidCardInfoVO;
            } catch (Exception e) {
                BaseLogUtils.error(logger, "-", "异常", e);
                BaseLogUtils.info(logger, "validateCstCert", "第三方验证身份证异常!");
            }
        } else {
            cstValidCardInfoVO.setSuccess(true);// 默认值
            BaseLogUtils.info(logger, "validateIdCard", "身份证功能未开启!");
        }
        return cstValidCardInfoVO;
    }

    //设置证件照片
    private void setCstCertImgInfo(List<CstCertRecDTO> list) {
        // 这里要获取客户身份证照片附件
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> pkList = new ArrayList<>();
            String certNo = "";
            for (CstCertRecDTO cstCertRecDTO : list) {
                certNo = cstCertRecDTO.getCertNo();
                if (pkList.contains(certNo)) {
                    continue;
                }
                pkList.add(certNo);
            }

            String customerServiceCd = PropertiesCacheUtils.getProperty(SystemConstant.FL_CUSTOMER_SERVICE_CD);
            SysUlFilesRecQueryCondition mySelf = new SysUlFilesRecQueryCondition();
            mySelf.setSysCd(customerServiceCd);
            mySelf.setPkIdList(pkList);
            mySelf.setTabNm(PropertiesCacheUtils.getProperty(TableConstant.CST_CERT_REC));
            mySelf.setCfgCd(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.UploadConstant.CFG_CST_CERT_CD));
            mySelf.setCfgDtlCd(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.UploadConstant.CFG_DTL_CST_CERT_CD));
            List<SysUlFilesRecDTO> mySelfList = this.uploadApiService.searchSysUlFilesRecList(mySelf);

            Map<String, String> map = new HashMap<>();
            if (CollectionUtils.isNotEmpty(mySelfList)) {
                String pkId = "";
                String path = "";
                for (SysUlFilesRecDTO sysUlFilesRecDTO : mySelfList) {
                    pkId = sysUlFilesRecDTO.getPkId();
                    path = sysUlFilesRecDTO.getFilePath();
                    map.put(pkId, path);
                }

                for (CstCertRecDTO cstCertRecDTO : list) {
                    cstCertRecDTO.setImgUrl(map.get(cstCertRecDTO.getCertNo()));
                }
            }
        }
    }

    /**
     * 新增客户照片
     *
     * @param cstBscInfoVO
     * @param id
     */
    private void uploadCstIDImg(String imgUrl, String certNm, String certNo, Long id) {
        List<SysUlFilesRecDTO> addList = new ArrayList<>();//需要新加的附件内容
        SysUlFilesRecDTO sysUlFilesRecDTO = null;
        String rootPath = PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH);
        if (StringUtils.isNotEmpty(imgUrl)) {
            sysUlFilesRecDTO = new SysUlFilesRecDTO();
            sysUlFilesRecDTO.setCrtTm(new Date());
            sysUlFilesRecDTO.setTabNm(PropertiesCacheUtils.getProperty(TableConstant.CST_CERT_REC));
            sysUlFilesRecDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            sysUlFilesRecDTO.setPkId(certNo);
            sysUlFilesRecDTO.setFileNm(certNm);
            sysUlFilesRecDTO.setFilePath(imgUrl);
            sysUlFilesRecDTO.setIsDel(BaseConstant.IS_YESNO_NO);
            sysUlFilesRecDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
            sysUlFilesRecDTO.setTransFlg(BaseConstant.IS_YESNO_NO);
            sysUlFilesRecDTO.setPInstId("");
            sysUlFilesRecDTO.setTInstId("");
            sysUlFilesRecDTO.setCfgCd(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.UploadConstant.CFG_CST_CERT_CD));
            sysUlFilesRecDTO.setCfgDtlCd(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.UploadConstant.CFG_DTL_CST_CERT_CD));
            sysUlFilesRecDTO.setFileRem("客户身份证照片,证件号码：" + certNo);
            sysUlFilesRecDTO.setOrgCd(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.UploadConstant.CFG_CST_CERT_ORG_CD));
            sysUlFilesRecDTO.setAbsLocPath(rootPath + imgUrl);
            addList.add(sysUlFilesRecDTO);
        }
        try {
            this.sysUlFilesRecService.addSysUlFilesRecListWithIsDel(addList, true);
        } catch (Exception e) {
            BaseLogUtils.error(logger, "uploadCstIDImg", "上传附件失败!" + e.getCause().getMessage());
        }
    }

    /**
     * 获取附件保存路径,不带最后一个文件名称的路径
     *
     * @param modelName
     * @return
     */
    private String getFileSavePath(String modelName) {
        String prifixPath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_ROOT_PATH);//根目录
        StringBuffer savePath = new StringBuffer(prifixPath);//最终保存路径
        if (!savePath.toString().endsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)) {//判断是否以"/"结尾,不是加入斜杠
            savePath.append(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH);
        }
        savePath.append(PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_COSTOMER_PATH));//自定义代码-其中包括系统代码
        if (StringUtils.isNotEmpty(modelName)) {//如果不等于空则加入
            if (modelName.startsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)) {//判断是否以"/"开头
                savePath.append(modelName.substring(1));
            } else {
                savePath.append(modelName);
            }

        }
        if (!savePath.toString().endsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)) {//判断是否以"/"结尾
            savePath.append(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH);
        }
        String currentTime = DateUtils.formatDateToString(new Date(), DateUtils.YYYYMMDD);
        savePath.append(currentTime);
        savePath.append(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH);
        //检查目录
        File pathFile = new File(savePath.toString());
        if (!pathFile.exists()) {
            //文件夹不存 创建文件
            boolean createPath = pathFile.mkdirs();
            if (!createPath) {
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "服务器附件目录未创建成功!" + savePath.toString());
            }
        }
        return savePath.toString();
    }

    /**
     * 设置客户证件照片
     */
    private void setCstIDImg(CstBscInfoDTO cstBscInfoDTO, Long id) {
        if (null != cstBscInfoDTO) {
            if (!CustomerConstant.CST_TYP_CD_NP.equals(cstBscInfoDTO.getCstTypCd())) {
                return;
            }

            /**
             * 自然人客户照片(CFG:CST002 、CFG_DTL_CD:10)
             */
            String mySelfIDImgUrl = "";
            String mySelfPOImgUrl = "";
            String certNo = cstBscInfoDTO.getCertNo();
            String peiouCertNo = "";//配偶证件号
            try {
                List<String> pkList = new ArrayList<>();
                pkList.add(certNo);
                String customerServiceCd = PropertiesCacheUtils.getProperty(SystemConstant.FL_CUSTOMER_SERVICE_CD);
                String serverPath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_SERVER_PATH);
                CstNpBscInfoDTO cstNpBscInfoDTO = cstBscInfoDTO.getCstNpBscInfoDTO();
                if (null != cstNpBscInfoDTO) {
                    CstNpSocialRlDTO cstNpSocialRlDTO = cstNpBscInfoDTO.getCstNpSocialRlDTO();
                    if (null != cstNpSocialRlDTO) {
                        peiouCertNo = cstNpSocialRlDTO.getCertNo();
                        if (StringUtils.isNotEmpty(peiouCertNo)) {
                            if (!pkList.contains(peiouCertNo)) {
                                pkList.add(peiouCertNo);
                            }
                        }
                    }
                }
                SysUlFilesRecQueryCondition mySelf = new SysUlFilesRecQueryCondition();
                mySelf.setSysCd(customerServiceCd);
                mySelf.setPkIdList(pkList);
                mySelf.setTabNm(PropertiesCacheUtils.getProperty(TableConstant.CST_CERT_REC));
                mySelf.setCfgCd(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.UploadConstant.CFG_CST_CERT_CD));
                mySelf.setCfgDtlCd(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.UploadConstant.CFG_DTL_CST_CERT_CD));
                List<SysUlFilesRecDTO> mySelfList = this.uploadApiService.searchSysUlFilesRecList(mySelf);

                if (CollectionUtils.isNotEmpty(mySelfList)) {
                    for (SysUlFilesRecDTO sysUlFilesRecDTO : mySelfList) {
                        String pkId = sysUlFilesRecDTO.getPkId();
                        if (certNo.equals(pkId)) {
                            mySelfIDImgUrl = sysUlFilesRecDTO.getFilePath();
                            if (!mySelfIDImgUrl.contains(serverPath)) {
                                mySelfIDImgUrl = serverPath + mySelfIDImgUrl;
                            }
                        }

                        if (StringUtils.isNotEmpty(peiouCertNo) && peiouCertNo.equals(pkId)) {
                            mySelfPOImgUrl = sysUlFilesRecDTO.getFilePath();
                            if (!mySelfPOImgUrl.contains(serverPath)) {
                                mySelfPOImgUrl = serverPath + mySelfPOImgUrl;
                            }
                        }
                    }
                }
                cstBscInfoDTO.setMySelfIDImgUrl(mySelfIDImgUrl);
                cstBscInfoDTO.setMyPeiOuIDImgUrl(mySelfPOImgUrl);
            } catch (BaseException e) {
                BaseLogUtils.info(logger, "setCstIDImg", "获取自然人身份照片失败！" + e.getMessage());
            } catch (Exception e) {
                BaseLogUtils.error(logger, "setCstIDImg", "获取自然人身份照片失败！" + e.getCause().getMessage());
            }
        }
    }

    @Override
    public JSONArray searchCurrentOrgWithChildTree() throws BaseException {
        String sysCd = CurrentThreadContext.getFlServiceSysCd();
        Long orgId = CurrentThreadContext.getCurrentOrgId();
        String orgCd = CurrentThreadContext.getCurrentOrgCd();
        List<Long> orgIds = new ArrayList<>();
        orgIds.add(orgId);
        SysOrgDTO currentOrgDTO = this.sysOrgApiService.selectSysOrgById(orgId, sysCd, orgCd);
        List<SysOrgDTO> orgChildList = this.sysOrgApiService.searchRecuOrgWithchildBy(orgIds, sysCd);
        JSONArray jsonArray = new JSONArray();
        JSONArray treeList = new JSONArray();
        if (CollectionUtils.isNotEmpty(orgChildList)) {
            for (SysOrgDTO sysOrgDTO : orgChildList) {
                if (StringUtils.isNotEmpty(sysOrgDTO.getIsDel()) && BaseConstant.IS_YESNO_YES == sysOrgDTO.getIsDel().intValue()) {
                    continue;
                }
                JSONObject obj = new JSONObject();
                obj.put("id", sysOrgDTO.getId());
                obj.put("tCode", sysOrgDTO.getOrgCd());
                obj.put("tName", sysOrgDTO.getOrgDispNm());
                obj.put("pid", sysOrgDTO.getOrgPid());
                jsonArray.add(obj);
            }
            Long ininPid = currentOrgDTO.getOrgPid();
            treeList = this.treeList(jsonArray, String.valueOf(ininPid));
            System.out.println("====>" + JSON.toJSONString(treeList));
        }
        return treeList;
    }

    @Override
    public List<CstBscInfoVO> searchListByCon(CstBscInfoQueryCondition condition)
            throws BaseException {
        List<CstBscInfoDTO> cstBscInfoDTOList = this.customerApiService.searchListByCon(condition);
        return CstBscInfoVOTransform.toVOList(cstBscInfoDTOList);
    }

    @Override
    public DataResultVO removeCertBind(String certNo, Long currentUserId) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "解绑成功！");
        Date currentDate = new Date();//当前时间
        //查询该用户是否已在客户表中注册
        CstBscInfoQueryCondition condition = new CstBscInfoQueryCondition();
        condition.setCertNo(certNo);
        List<CstBscInfoDTO> cstBscInfoDTOList = customerQueryApiService.searchCstBscInfoList(condition);
        if (cstBscInfoDTOList != null && cstBscInfoDTOList.size() > 0) {//存在注册客户
            CstBscInfoDTO cstBscInfoDTO = cstBscInfoDTOList.get(0);
            Long userId = cstBscInfoDTO.getUsrId();
            if (null != userId) {//已注册则在用户表及关系表中删除该用户信息
                boolean success = sysUsrModifyApiService.modifySysUsrDelStatus(userId, currentDate, currentUserId);
                if (success) {//更新客户表中用户id为null
                    this.customerApiService.modifyUsrIdById(cstBscInfoDTO.getId(), null, currentDate, currentUserId);
                }
            } else {
                result.setSuccess(BaseConstant.IS_YESNO_YES);
                result.setInfo("该客户已经解绑证件");
            }
        } else {
            result.setSuccess(BaseConstant.IS_YESNO_YES);
            result.setInfo("该客户还未绑定证件");
        }
        return result;
    }

    @Override
    public Pager searchAllListPage(Pager page) {
        if (null == page) page = new Pager();
        page = this.customerApiService.searchCstBscInfoAllListPage(page);
        List<CstBscInfoDTO> list = (List<CstBscInfoDTO>) page.getResultList();
        page.setResultList(CstBscInfoVOTransform.toVOList(list));
        return page;
    }

    @Override
    public List<CstCorpBscInfoVO> searchCstCorpBscInfoVOListByConditon(CstCorpBscInfoQueryCondition condition) throws BaseException {
        return CstCorpBscInfoVOTransform.toVOList(this.customerQueryApiService.selectCstCorpListByCondition(condition));
    }

    /**
     * 同步修改基础信息 新增修改主表 修改详细表
     */
    @Override
    public Long addCstInfoMod(MfrInfoModDTO mfrInfoModDTO, CstBscInfoDTO dto) throws BaseException {
        // TODO Auto-generated method stub
        if (null == mfrInfoModDTO || null == dto) {
            BaseLogUtils.info(logger, "addCstInfoMod", "同步修改基础信息 新增修改主表 修改详细表  参数不能为空!", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.mfrInfoModApiService.addCstInfoMod(mfrInfoModDTO, dto, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 客户主要信息变更流程中-再次修改（呈报人节点）
     */
    @Override
    public boolean modifyCstBscAgain(CstBscInfoDTO entity, String modId) throws BaseException {
        if (null == entity || StringUtils.isEmpty(modId)) {
            BaseLogUtils.info(logger, "modifyCstBscAgain", "修改客户失败,客户对象参数为空.DATA:" + com.alibaba.fastjson.JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        MfrInfoModDTO mfrInfoModDTO = this.mfrInfoModApiService.selectMfrInfoModDTOById(Long.valueOf(modId), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), "", "");
        if (null != mfrInfoModDTO) {
            return this.mfrInfoModApiService.modifyCstBscAgain(entity, Long.valueOf(modId), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CurrentThreadContext.getCurrentUserId(), CurrentThreadContext.getCurrentUserName());
        }
        return true;
    }

    /**
     * 客户主要信息变更流程-获取客户信息
     */
    @Override
    public CstBscInfoVO selectCstFlowById(Long id, Long modId, String buOrgCd) throws BaseException {
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(modId)) {
            BaseLogUtils.info(logger, "selectMfrFlowById", "客户主要信息变更流程-获取客户数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        CstBscInfoVO cstBscInfoVO = this.selectBy(id, buOrgCd);
        MfrInfoModDTO mfrInfoModDTO = this.mfrInfoModApiService.selectMfrInfoModDTOById(modId, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), "", "");
        MfrInfoModDtlQC mfrInfoModDtlQC = new MfrInfoModDtlQC();
        mfrInfoModDtlQC.setModId(modId);
        mfrInfoModDtlQC.setIsDel(BaseConstant.IS_YESNO_NO);
        List<MfrInfoModDtlDTO> mfrInfoModDtlDTOs = this.mfrInfoModDtlApiService.searchMfrInfoModDtlDTOList(mfrInfoModDtlQC);
        try {
            if (null != mfrInfoModDTO) {
                //判断客户类型：1 自然人;2 法人
                if (null != cstBscInfoVO.getCstTypCd()) {
                    if ("1".equals(cstBscInfoVO.getCstTypCd())) {//自然人
                        //获取自然人客户基础信息-变更前
                        CstNpBscInfoVO npBscInfoVO = cstBscInfoVO.getCstNpBscInfoVO();
                        //获取配偶信息-变更前
                        CstNpSocialRlVO npSocialRlVO = npBscInfoVO.getCstNpSocialRlVO();
                        //遍历变更详细表
                        for (MfrInfoModDtlDTO mfrInfoModDtlDTO : mfrInfoModDtlDTOs) {
                            //判断变更类型
                            if (CommonOpTypCdEnum.MDF.getOpTypCd().equals(mfrInfoModDtlDTO.getModTypCd())) {  //修改
                                //判断变更记录表名称
                                if (CustomerConstant.CST_BSC_INFO_TAB.equals(mfrInfoModDtlDTO.getTabNm())) { //1.客户基础信息表
                                    Field field = CstBscInfoVO.class.getDeclaredField(mfrInfoModDtlDTO.getModAttrFiled());
                                    field.setAccessible(true);
                                    field.set((Object) cstBscInfoVO, mfrInfoModDtlDTO.getModConAft());
                                } else if (CustomerConstant.CST_NP_BSC_INFO_TAB.equals(mfrInfoModDtlDTO.getTabNm())) { //2.自然人客户基础信息表
                                    Field field = CstNpBscInfoVO.class.getDeclaredField(mfrInfoModDtlDTO.getModAttrFiled());
                                    field.setAccessible(true);
                                    field.set((Object) npBscInfoVO, mfrInfoModDtlDTO.getModConAft());
                                } else if (CustomerConstant.CST_NP_SOCIAL_RL_TAB.equals(mfrInfoModDtlDTO.getTabNm())) { //3.自然人社会关系信息表-配偶信息
                                    Field field = CstNpSocialRlVO.class.getDeclaredField(mfrInfoModDtlDTO.getModAttrFiled());
                                    field.setAccessible(true);
                                    field.set((Object) npSocialRlVO, mfrInfoModDtlDTO.getModConAft());
                                }
                            }
                        }
                    } else if ("2".equals(cstBscInfoVO.getCstTypCd())) {//法人
                        //获取法人客户基础信息-变更前
                        CstCorpBscInfoVO corpBscInfoVO = cstBscInfoVO.getCstCorpBscInfoVO();
                        //遍历变更详细表
                        for (MfrInfoModDtlDTO mfrInfoModDtlDTO : mfrInfoModDtlDTOs) {
                            //判断变更类型
                            if (CommonOpTypCdEnum.MDF.getOpTypCd().equals(mfrInfoModDtlDTO.getModTypCd())) {  //修改
                                //判断变更记录表名称
                                if (CustomerConstant.CST_BSC_INFO_TAB.equals(mfrInfoModDtlDTO.getTabNm())) { //1.客户基础信息表
                                    Field field = CstBscInfoVO.class.getDeclaredField(mfrInfoModDtlDTO.getModAttrFiled());
                                    field.setAccessible(true);
                                    field.set((Object) cstBscInfoVO, mfrInfoModDtlDTO.getModConAft());
                                } else if (CustomerConstant.CST_CORP_BSC_INFO_TAB.equals(mfrInfoModDtlDTO.getTabNm())) { //4.法人客户基础信息表
                                    Field field = CstCorpBscInfoVO.class.getDeclaredField(mfrInfoModDtlDTO.getModAttrFiled());
                                    field.setAccessible(true);
                                    field.set((Object) corpBscInfoVO, mfrInfoModDtlDTO.getModConAft());
                                }
                            }
                        }
                    }
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return cstBscInfoVO;
    }

    /**
     * @MethodName selectCstCertNoNm
     * @Description: 通过id获取客户基本信息
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2020/3/3 17:22
     * @Version: V1.0.0
     * @Param id
     * @Return cn.fl.customer.vo.CstBscInfoVO
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public CstBscInfoDTO selectCstCertNoNm(Long id) throws BaseException {
        if(StringUtils.isEmpty(id)){
            BaseLogUtils.info(logger,"selectCstCertNoNm","客户ID不能为空");
            throw new BaseException("客户id 不能为空");
        }
        CstBscInfoQueryCondition qc=new CstBscInfoQueryCondition();
        qc.setCstIds(id.toString());
        return this.customerApiService.selectCstCertNoNm(qc);
    }

    @Override
    public CstBscInfoDTO selectCstBscInfoById(Long id, String buOrgCd) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        CstBscInfoDTO dto = this.customerApiService.selectCstBscInfoById(id, buOrgCd);
        this.setCstIDImg(dto, id);
        // 不处理客户关系
        return dto;
    }

    @Override
    public boolean modifyCstBscInfo(CstBscInfoDTO entity) throws BaseException {
        if (null == entity) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        this.setCstUsrRDTOInfo(entity);
        boolean flag = this.customerApiService.modifyCstBscInfo(entity);
        return flag;
    }

    /**
     * 设置客户-用户关系信息
     *
     * @param entity
     */
    private void setCstUsrRDTOInfo(CstBscInfoDTO entity) {
        CstUsrRDTO cstUsrRDTO = new CstUsrRDTO();
        cstUsrRDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
        cstUsrRDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        cstUsrRDTO.setOpTm(new Date());
        cstUsrRDTO.setOpUsrId(CurrentThreadContext.getCurrentUserId());
        entity.setCstUsrRDTO(cstUsrRDTO);
    }
}
